package oldVersion.storage;


/**
 *@author Adam
 */


import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Vector;

import javax.swing.JTextArea;
import org.farng.mp3.MP3File;
import org.farng.mp3.TagException;
import org.farng.mp3.id3.ID3v2_2;


public class GuiStorage implements Storage{

	public String host = "";
	public int port = -1;
	public String serviceName = "";
	public static final String SERVICE_TYPE = "uss.local.";
	public TCPSocketStorage TCPSS;
	public String RMIServerIP = "";
	public RMIClient rmic = new RMIClient(this);
	public Vector songs = new Vector();
	private JTextArea msg;


	public GuiStorage(JTextArea msg) {
		this.msg=msg;
	}

	public void createXML(){

	}

	public void setRMIServerIP(String s){
		RMIServerIP =s;
	}

	public void openRMI(){
		rmic.begin(RMIServerIP);
	}

	public Vector getSongs(){
		return songs;
	}

	public void populateVector(){
		try{ 
			File dir = new File("src/mp3s/");
			String[] children = dir.list();
			if (children == null) {
				//do nothing
			} else {
				InetAddress thisIp =InetAddress.getLocalHost();
				System.out.println("IP:"+thisIp.getHostAddress());
				msg.append("IP:"+thisIp.getHostAddress()+"\n");
				for (int i=0; i<children.length; i++) {					
					// Get filename of file or directory
					String filename = children[i];
					System.out.println(filename);
					msg.append(filename+"\n");
					if (filename.equals(".svn") || filename.startsWith("tu")){
						//do nothing
					}
					else{
						File sourcefile = new File("src/mp3s/" + filename);
						MP3File mp3file = new MP3File(sourcefile);
						ID3v2_2 t = (ID3v2_2) mp3file.getID3v2Tag();
						String artist = t.getLeadArtist();
						String title = t.getSongTitle();
						String album = t.getAlbumTitle();
						String location = thisIp.getHostAddress();
						SongObject so = new SongObject(artist,album,title,location);
						System.out.println(so.toString());
						msg.append(so.toString()+"\n");
						songs.addElement(so);

					}
				}
			}
		} catch (IOException e) {
			System.out.println("problem io in xml making");
			msg.append("problem io in xml making\n");
			e.printStackTrace();
		} catch (TagException e) {
			System.out.println("problem tagging in xml making");
			msg.append("problem io in xml making\n");
			e.printStackTrace();
		}
	}

	public void findServices(){
		BonjourListener sl = new BonjourListener(SERVICE_TYPE, this); //find the service
	}

	public void serviceDiscovered(String myHost, int myPort, String myServiceName){
		msg.append("setting up for socket");
		System.out.println("setting up for socket");
		this.host = myHost;
		this.port = myPort;
		serviceName = myServiceName;
		TCPSS = new TCPSocketStorage(host,port, this);
		String s = findRMIServer();
		TCPSS.sendMessage(s);
	}

	public String findRMIServer(){
		return "<RMIServer>where</RMIServer>";
	}

	public String getMp3List(){
		DataInputStream inputStream = null; 
		String s = null;
		String overall = "";
		try { 
			File f = new File("src/storage/mp3s.xml"); 
			FileInputStream filein = new FileInputStream(f); 
			BufferedInputStream bufferin = new BufferedInputStream(filein); 
			inputStream = new DataInputStream(bufferin);  
			while ( (s=inputStream.readLine()) != null ) {  
				//System.out.println(s);
				overall += s + "&nbsp;";

			} 
		} catch (IOException er) {
			msg.append("problem reading file"+ er.getMessage()+"\n");
			System.out.println("problem reading file" + er.getMessage()); 
		}
		return overall;
	}


	public static void main(String[] args){
		Monitor m=new Monitor();
		m.setVisible(true);
		Thread t=new Thread(m);
		t.start();
		GuiStorage ts = new GuiStorage(m.getTxtRef());
		ts.populateVector();
		ts.findServices();
		RTPServer rtps = new RTPServer(m.getTxtRef());
		rtps.start();
	}
}